var currPage=1;
var pageSize=8;
var memberId;
var count=0;

$(function(){
	member=getEntity("memberInfo");
	init(member.mId,pageSize,currPage);
	//点击下一页事件
	$("#page").click(function(){
		currPage=$(".pageItemActive").attr("page-data");
		init(member.mId,queryStatus,pageSize,currPage);
	});
});
var index = 0;
//收藏
$('.jilu-colec').click(function(){
	var textC=$(this).text();
	if(textC=='编辑'){
		$(this).text('完成');
	    $('.evaluateGoodsIO').css({display:'block'});
	    $('.footprintE').css({display:'block'});
	    $('.footprintE input').css({display:'block'});
	    $('.footprintE a:nth-child(5)').click(function(){
	    $('.jilu-colec').text('编辑');	
	    $('.footprintE').css({display:'none'});
	  });
	}else{
		 $(this).text('编辑');
		 $('.goodss .box').prop('checked', false);
		 $('.jiesuan img').attr('src',rootPath+'/image/yuan1.png');
		 $('.footprintE input ').prop('checked', false);
		 $('.evaluateGoodsIO').attr('src',rootPath+'/image/yuan1.png');
		 $('.evaluateGoodsIO').css({display:'none'});
		 $('.footprintE').css({display:'none'});
		 };
	
});
//初始化请求收藏列表
function init(memberId,pageSize,currPage){
	$.ajax({
		type:"post",
		url:rootPath+"/favorites/selectByPage",
		data:{'mId':member.mId,'collectionType':1,'currPage':currPage,'pageSize':pageSize},
		dataType:"JSON",
		success:function(data){
			$("#collections").empty();
			count=data.count;
			var str='';
			var collections=data.data;
			$.each(collections,function(i,item){
				str += createHtml(item,i);
			});
			$("#collections").append(str);
		}	
	});
}

//创建收藏列表
function createHtml(item,i){
	var str='';
	var marketPrice=parseFloat(item.marketPrice);
	str+='<li class="goodss">'
				+'<input type="checkbox" class="box" value="'+item.id+'"/>'
				+'<img src="'+rootPath+'/image/yuan1.png" alt="" class="evaluateGoodsIO"/>'
				+'<img src="'+item.img+'" alt="'+item.name+'" class="evaluateGoodsI evaluateGoodsM"/>'
				+'<a href=" '+rootPath+'/goods/queryGoodsDetail?id='+item.goodsId+'"><div class="evaluateGoodsW">'
				+'	<p class="moreWord">'+item.name+'</p>'
				+'	<p class="prices">'
				+'	<b>¥ </b><span>'+marketPrice.toFixed(2)+'</span>'
				+'</p>'
				+'</div></a>'
			+'</li>';
	return str;
}
var check = document.querySelectorAll('.box');	
var imgss = document.querySelectorAll('.evaluateGoodsIO');
var del = document.querySelectorAll('.footprintE a:nth-child(4)')[0];
var liss= document.querySelectorAll('.footprint li');
//全选
$('.footprintE input').click(function(){
	var check=$('.goodss .box').prop('checked', this.checked);
	/** 全选全不选*/
	if(this.checked==true){
	    $('.jiesuan img').attr('src',rootPath+'/image/yuan2.png');
		$('.evaluateGoodsIO').attr('src',rootPath+'/image/yuan2.png');
		  
	}else if(this.checked!=true){
	    $('.jiesuan img').attr('src',rootPath+'/image/yuan1.png');
		$('.evaluateGoodsIO').attr('src',rootPath+'/image/yuan1.png');
	}
	
});	

$(document).on("click",".goodss .box",function(){
	console.log($('.footprintE input'));	
	//必要时更新全选按钮
	var allCheckBoxCount = $('.goodss .box').length;
	var checkedCount = $('.goodss .box:checked').length;
	var $checked = checkedCount==allCheckBoxCount;
	if($checked){	
		$('.jiesuan img').attr('src',rootPath+'/image/yuan2.png');	//全选选中
		$('.footprintE input ').prop('checked', true);	//全选选中
	}else {
		$('.jiesuan img').attr('src',rootPath+'/image/yuan1.png');	//全选不选中
		$('.footprintE input ').prop('checked', false);	//全选不选中
	}
     
	/**check单选框*/
    if($(this).prop('checked')){
    	$(this).prop('checked',true);
		$(this).next().attr('src',rootPath+'/image/yuan2.png');
	}else if(!$(this).prop('checked')){
		$(this).prop('checked',false);
		$(this).next().attr('src',rootPath+'/image/yuan1.png');
	} 
		
});

$('.jiesuan a:nth-child(4)').click(function(){
    $('.footprint').remove();
   /* $('.footprintE').css({display:'none'});*/
    $('.jilu').text('编辑');
});

var srcPath = rootPath+'/image/yuan2.png';
/**
 * 
 * 删除所勾选数据
 * 
 */
function deleteFavorites(){
	var chk_value=[];
	var arrUrl = $(".evaluateGoods>li");
	$.each(arrUrl,function(){
		var src = $(this).find(".evaluateGoodsIO").attr("src")
		var val = $(this).find(".box").val();
		if(srcPath == src){
			chk_value.push(val);	
		}
	});
	/** 为选择商品的时候给出提示*/
	if(chk_value.length<1){
		alert("请选择要删除的商品！");
		return false;
	}
	var shanchu = confirm('确认要删除该宝贝吗？');
	if(shanchu == true){
		$.ajax({
			url : rootPath+"/favorites/deleteFavorites",
			type : "POST",
			data : {"fids":""+chk_value+""},
			async: false,
			success: function(data,status){
				if(data.resultSuccess == true ){
				    alert("删除成功！");	
					window.location.reload();
				}else if(data.resultSuccess != true){
					alert("删除失败");
   				};
			},	
			error: function(xhr,status,e){
				alert("服务器出了问题，请检查！"+e);
			}	
		});
	}

}



function tt(dd){
    //alert(dd);
}

var currentPage = 2;
/**收藏分页***/
function clickRefresh(){
	// 加载数据
    $.ajax({
        type: 'POST',
        url : rootPath+"/favorites/queryConsult.do",
        data : {"currPage":currentPage,"type":1,"mId":member.mId,"pageSize":pageSize},
      	async : false	,
        success: function(data,status){
        	currentPage =data.currentPage;
        	currentPage = parseInt(currentPage);
        	currentPage++;
         	var resultList = data.data;
        	var totalRecords = data.totalRecords; //总条数
        	if(resultList!=null && resultList.length>0){
    			var optionHtml = '';
    			$.each(resultList,function(i,result){
					optionHtml+="<li> "
						+" <input type='checkbox' value='"+result.id+"' class='check'/> "
						+" <img src='"+rootPath+"/image/yuan1.png' alt='' class='evaluateGoodsIO'/> "
						+" <img src='"+rootPath+"/image/pic184.png' alt='' class='evaluateGoodsI'/> "
						+" <div class='evaluateGoodsW'> "
						+" <p class='wordes moreWord'>"+result.name+"</p> "
						+" <p class='prices'> "
						+" <b>¥ "+result.marketPrice+"</b> "
						+" </p> "
						+" </div> "
						+" </li>";
				});
        	}else{ 
        		$(".pageRefresh").text("数据加载完毕！");
   	}	
        	$(".mycolle").append(optionHtml);
       	},
        error: function(xhr, type){
            alert('Ajax error!');
        }
    })
}
 